Making Code by unknow

Making Code by unknow

Author:unknow
Language: eng
Format: epub
Publisher: php[architect]
Published: 2022-09-15T00:00:00+00:00


Go Build Stuff

There are a few other patterns that help build objects as well. One is the Abstract Factory, which are factories that take in other factories to generate objects. If we wanted to be able to represent pages on our site in various ways, we might make a PageFactory, which has a createDigitalView() method and a createPrintView() method. We may make a MobileDeviceViewFactory that creates a mobile-specific view and printable PDF for a user, a DesktopViewFactory that does the same thing but for larger screens, and a TerminalViewFactory that creates raw text output as well as physically prints to an attached printer.

Another is the Prototype pattern. This is where a prototype object is created and can be copied and modified for use. In PHP, we can use the clone keyword to copy an existing object to a new copy and edit it. This is different than $objectB = $objectA, as PHP assigns objects to variables via reference by default. In this case, $objectA and $objectB are not copies but are the same object. $objectB = clone $objectA makes a discreet copy.

Singletons are also considered part of this “object creation” batch of patterns. It enforces one global instantiation of an object, but for the sake of what we are talking about, it does not help you “build new objects.”

Take a look at your code and see where Factories or Builders may help out. If you see a pattern when it comes to creating objects, maybe one of these patterns will be a better fit. At least until the lion gets you.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.